home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 / Ham Radio 2000.iso / ham2000 / morse / txt2eprm / morse.bas < prev    next >
BASIC Source File  |  1993-05-26  |  17KB  |  378 lines

  1.  '  THE PROGRAM FILES MORSE.EXE, MORSE.BAS AND CW.DAT ("THE SOFTWARE") ARE
  2.  '  OWNED BY VE2HOT.  THE PROGRAM OWNER HEREBY ALLOWS YOU TO:
  3.  '
  4.  '      1) USE THE SOFTWARE
  5.  '      2) MAKE AS MANY COPIES OF THE SOFTWARE AS YOU WISH
  6.  '      3) GIVE COPIES OF THE SOTWARE TO ANYONE
  7.  '      4) DISTRIBUTE THE SOFTWARE VIA ELECTRONIC MEANS
  8.  '
  9.  '  YOU  ARE  SPECIFICALLY    PROHIBITED   FROM  CHARGING,  OR  REQUESTING
  10.  '  DONATIONS, FOR ANY SUCH  COPIES,  HOWEVER MADE;  AND FROM DISTRIBUTING
  11.  '  THE SOFTWARE WITH COMMERCIAL PRODUCTS  WITHOUT  PRIOR  PERMISSION.  NO
  12.  '  ONE IS AUTHORIZED TO CHARGE ANY  AMOUNT  FOR DISTRIBUTION OF COPIES OF
  13.  '  THE SOFTWARE, OR TO INCLUDE COPIES OF THE SOFTWARE WITH SALES OF THEIR
  14.  '  OWN PRODUCTS.  THIS NOTICE MAY NOT BE REMOVED OR MODIFIED IN ANY WAY.
  15.  '
  16.  '  THERE IS NO WARRANTY OF ANY KIND.   IN  NO  EVENT  SHALL  THE SOFTWARE
  17.  '  OWNER BE LIABLE FOR ANY DAMAGES, DIRECT OR CONSEQUENTIAL,  ARISING OUT
  18.  '  OF THE USE OF OR INABILITY TO USE THIS PRODUCT, EVEN IF THE AUTHOR HAS
  19.  '  BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.  BY USING  THIS  FREE
  20.  '  SOFTWARE, YOU AGREE TO THIS.
  21.  '
  22. 10 '-----------------------------------------------------------------
  23. 20 '      C O N S T A N T S    A N D     D E F I N I T I O N S
  24. 30 '-----------------------------------------------------------------
  25. 35 CLS
  26. 40 PRINT ""
  27. 50 PRINT "TEXT to CW to EPROM conversion program          Dec 1990"
  28. 60 PRINT "Written for Nick Ciarallo [VE2HOT] by K.C. Ng Ching Hing"
  29. 70 PRINT "________________________________________________________"
  30. 80 PRINT ""
  31. 90 PRINT "Initializing. Please wait..."
  32. 100 MAXBITS% = 7
  33. 110 MAXSTRING% = 4000                  'number of bits in string
  34. 120 NULL% = -32768!                    'flag for commands
  35. 130 DIM ASCII$(127)                    'ascii code lookup table
  36. 140 DIM S%(MAXBITS%, MAXSTRING%)       'bit array 1
  37. 150 DIM COMMMANDFLAG%(MAXBITS%)        'array of command flags, one per line
  38. 160 DIM CURRADDR%(MAXBITS%)            'current bit in eprom
  39. 170 FALSE% = 0
  40. 180 TRUE% = -1
  41. 190 DATATOKEN$ = "data"                'string signifying data follows in input file
  42. 200 ESC$ = "\"                         'escape character
  43. 210 INTERACTIVE% = FALSE%
  44. 220 DATAFILE$ = "cw.dat"
  45. 230 BINFILE$ = "cw.bin"                'default output file in binary
  46. 240 HEXFILE$ = "cw.hex"                'default output file in hexadecimal
  47. 250 AUDITFILE$ = "cw.aud"              'default output file in text form for audit
  48. 260 ASCIIFILE$ = "data.txt"            'default
  49. 270 '------------------------------------------------------------------
  50. 280 '             I N I T I A L I Z E    T H E    D A T A
  51. 290 '------------------------------------------------------------------
  52. 300 OPEN DATAFILE$ FOR INPUT AS #1
  53. 310 FOR I% = 1 TO 127                       'initialise the array
  54. 320    ASCII$(I%) = ""
  55. 330 NEXT
  56. 340 LIN% = 1                                'line number we are at
  57. 350 WHILE NOT EOF(1)                        'read the data file
  58. 360    LINE INPUT #1, S$
  59. 370    F$ = LEFT$(S$, 1)                    'first character in string
  60. 380    IF (F$ <> "") THEN GOTO 390 ELSE GOTO 500
  61. 390   'do
  62. 400       I% = ASC(F$)                      'save table index
  63. 410       A$ = MID$(S$, 2)                  'get next character
  64. 420       F$ = LEFT$(A$, 1)
  65. 430       WHILE (ASC(F$) <= ASC(" ")) OR (ASC(F$) > ASC("~")) 'skip blank characters
  66. 440          A$ = MID$(A$, 2)
  67. 450          F$ = LEFT$(A$, 1)
  68. 460          IF LEN(F$) = 0 THEN PRINT "error: invalid line in data file,"; DATAFILE$; " line"; LIN%, : GOTO 3540
  69. 470       WEND
  70. 480       ASCII$(I%) = A$
  71. 490      LIN% = LIN% + 1
  72. 500   'end if
  73. 510 WEND
  74. 520 CLOSE #1
  75. 530 PRINT ""
  76. 540 PRINT "Program start..."
  77. 550 PRINT ""
  78. 560 LINE INPUT "Enter filename to convert > "; A$
  79. 570 GOSUB 2760                         'extract name from file
  80. 580 ASCIIFILE$ = FILENAME$ + EXT$
  81. 590 OPEN ASCIIFILE$ FOR INPUT AS #2
  82. 600 PRINT "Converting text..."
  83. 610 '------------------------------------------------------------------
  84. 620 '       S T A R T    T O    C O N V E R T    T H E    T E X T
  85. 630 '------------------------------------------------------------------
  86. 640 'get user input
  87. 650  IF INTERACTIVE% = TRUE% THEN GOTO 660 ELSE GOTO 690
  88. 660 'do
  89. 670     PRINT "enter text > ";             'prompt
  90. 680     GOSUB 1200                         'my version of input a$
  91. 690 'endif
  92. 700  DONE% = FALSE%
  93. 710  FOR CURRLINE% = 0 TO MAXBITS%
  94. 720     CURRADDR%(CURRLINE%) = 1           'init bit position in s%
  95. 730  NEXT
  96. 740  CURRLINE% = 0                         'default: text is placed on this bit line
  97. 750  A$ = " "
  98. 760  WHILE (LEFT$(A$, 1) <> CHR$(27)) AND NOT EOF(2)
  99. 770     IF INTERACTIVE% = FALSE% THEN GOTO 780 ELSE GOTO 800
  100. 780    'do
  101. 790        LINE INPUT #2, A$
  102. 800    'endif
  103. 810     POSITION% = 1                       'first character in string
  104. 820     LENGTH = LEN(A$)
  105. 830     WHILE POSITION% <= LENGTH
  106. 840        CH% = ASC(MID$(A$, POSITION%, 1))'get the character in the table ascii$
  107. 850        IF (CH% >= ASC("a")) AND (CH% <= ASC("z")) THEN CH% = CH% - ASC("a") + ASC("A") ' convert to upper case
  108. 860        IF (CH% = ASC(ESC$)) THEN GOTO 870 ELSE GOTO 900 ' a command is present ?
  109. 870       'do                              'process command
  110. 880           GOSUB 2250                   'see what kind od command
  111. 890           GOTO 920
  112. 900       'else                            'process text
  113. 910           GOSUB 1650                   'scan database string and convert to bits
  114. 920       'endif
  115. 930        POSITION% = POSITION% + 1            'next character in user input
  116. 940     WEND                                    'for each string
  117. 950     IF INTERACTIVE% = TRUE% THEN GOTO 960 ELSE GOTO 1000
  118. 960    'do
  119. 970        PRINT "enter text > ";          'prompt
  120. 980        GOSUB 1200                      'line input from keyboard
  121. 990        GOTO 1000
  122. 1000    'endif
  123. 1010  WEND
  124. 1020  CLOSE #1
  125. 1030  CLOSE #2
  126. 1040  GOSUB 2680                            'find largest address
  127. 1050  A$ = " "
  128. 1060  WHILE (LEFT$(A$, 1) <> CHR$(27))
  129. 1070     INPUT "Do you want sound? (y/n) >", A$
  130. 1080     IF (A$ = "Y") OR (A$ = "y") THEN GOSUB 1510 ELSE A$ = CHR$(27) 'execute sound procedure
  131. 1090  WEND
  132. 1100  PRINT "Writing output files..."
  133. 1110  GOSUB 2940                            'write data in binary
  134. 1120  GOSUB 3090                            'write data in hex
  135. 1130  GOSUB 3260                            'write data in ascii
  136. 1140  IF INTERACTIVE% = TRUE% THEN PRINT
  137. 1150  PRINT "End of program"
  138. 1160  GOTO 3540
  139. 1170 '*****************************************************************
  140. 1180 '            E N D    O F    P R O G R A M    H E R E.
  141. 1190 '*****************************************************************
  142. 1200 '-----------------------------------------------------------------
  143. 1210 '               Subroutine to read the keyboard
  144. 1220 ' Includes backspaces.
  145. 1230 '-----------------------------------------------------------------
  146. 1240 STARTPOS% = POS(X)
  147. 1250 CH$ = ""
  148. 1260 CH% = 0
  149. 1270 A$ = ""
  150. 1280 QUIT = FALSE%
  151. 1290 WHILE NOT QUIT
  152. 1300    CH$ = INKEY$                        'get keyboard input
  153. 1310    ON LEN(CH$) + 1 GOTO 1490, 1320, 1470
  154. 1320   'regular keys
  155. 1330    CH% = ASC(CH$)
  156. 1340    IF CH% = 27 THEN QUIT = TRUE%: A$ = CH$: GOTO 1490 'user quits
  157. 1350    IF CH% = 13 THEN QUIT = TRUE%: GOTO 1380           'end of line
  158. 1360    IF CH% = 8 THEN GOTO 1400                          'backspace
  159. 1370    A$ = A$ + CH$
  160. 1380    PRINT CH$;                          'print character etc
  161. 1390    GOTO 1490
  162. 1400    IF POS(X) <= STARTPOS% THEN GOTO 1490
  163. 1410    LOCATE , POS(X) - 1                 'step back 1 character
  164. 1420    A$ = LEFT$(A$, LEN(A$) - 1)         'reduce string
  165. 1430    PRINT " ";                          'write a blank
  166. 1440    IF POS(X) < STARTPOS% THEN GOTO 1490
  167. 1450    LOCATE , POS(X) - 1                 'step back 1 character
  168. 1460    GOTO 1490
  169. 1470   'function keys
  170. 1480    GOTO 1490
  171. 1490 WEND
  172. 1500 RETURN
  173. 1510 '-----------------------------------------------------------------
  174. 1520 '      Subroutine to translate string into morse code (sound)
  175. 1530 '-----------------------------------------------------------------
  176. 1540 TONE% = 800
  177. 1550 DURATION% = 1!                        'seconds to sound speaker
  178. 1560 INPUT "Bit number to play ? (0..7) >", CURRLINE%
  179. 1570 IF (CURRLINE% >= 0) AND (CURRLINE% <= 7) THEN GOTO 1580 ELSE GOTO 1560
  180. 1580 IF CURRADDR%(CURRLINE%) = 0 THEN PRINT "No data for that bit": GOTO 1640
  181. 1590 FOR ADDR% = 0 TO CURRADDR%(CURRLINE%)
  182. 1600    VALUE% = S%(CURRLINE%, ADDR%)
  183. 1610    IF VALUE% = 1 THEN SOUND TONE%, DURATION%
  184. 1620    IF VALUE% = 0 THEN SOUND 32767, DURATION% 'sound nothing
  185. 1630 NEXT
  186. 1640 RETURN
  187. 1650 '-----------------------------------------------------------------
  188. 1660 '       Subroutine to convert ASCII to morse code in bits.
  189. 1670 ' The value of the character is stored in a table.  The table is
  190. 1680 ' encoded as 0 = a dit and 1 = a dah.
  191. 1690 ' Dits are one bit long (high).  Dahs are three bits long (high).
  192. 1700 ' The spacing between characters is 3 bits long (low).  The spacing
  193. 1710 ' between dits and dahs is 1 bit long (low).
  194. 1720 '    (a) If not first item in string, write space between characters.
  195. 1730 '    (b) Scan database table. (ASCII$)
  196. 1740 '        If 0 then
  197. 1750 '           if not first bit then write a 0
  198. 1760 '           write a dit
  199. 1770 '        else
  200. 1780 '           if not first bit then write a 0
  201. 1790 '           write a dah
  202. 1800 '-----------------------------------------------------------------
  203. 1810  IF (CURRADDR%(CURRLINE%) > 1) OR (CH% = ASC(" ")) GOTO 1820 ELSE GOTO 1870'determine if we should add spaces or not
  204. 1820 'do
  205. 1830  FOR J% = 0 TO 2                       'write sbc
  206. 1840     BITVALUE% = 0                      'fill with zeros
  207. 1850     GOSUB 2560                         'process this address
  208. 1860  NEXT
  209. 1870 'endif
  210. 1880  P% = 1
  211. 1890  L% = LEN(ASCII$(CH%))
  212. 1900  IF L% > 0 THEN GOTO 1910 ELSE GOTO 2230
  213. 1910  'do
  214. 1920  FIRST% = TRUE%                    'are we doing first bit in character?
  215. 1930  WHILE P% <= L%              'scan through the string held in ascii$(ch)
  216. 1940     V$ = LEFT$(MID$(ASCII$(CH%), P%), 1)
  217. 1950     IF V$ = "0" THEN GOTO 1960 ELSE GOTO 2060    'it's a dit
  218. 1960    'do
  219. 1970        IF FIRST% = FALSE% THEN GOTO 1980 ELSE GOTO 2010
  220. 1980        'do
  221. 1990           BITVALUE% = 0                'write interval
  222. 2000           GOSUB 2560                   'process this address
  223. 2010        'endif
  224. 2020        BITVALUE% = 1                   'write bit
  225. 2030        GOSUB 2560                      'process this address
  226. 2040        FIRST% = FALSE%
  227. 2050        GOTO 2200
  228. 2060    'else
  229. 2070        IF V$ = "1" THEN GOTO 2080 ELSE GOTO 2190   'it's a dah
  230. 2080       'do
  231. 2090           IF FIRST% = FALSE% THEN GOTO 2100 ELSE GOTO 2130
  232. 2100          'do
  233. 2110              BITVALUE% = 0             'write interval
  234. 2120              GOSUB 2560                'process this address
  235. 2130          'endif
  236. 2140           FOR J% = 0 TO 2
  237. 2150              BITVALUE% = 1             'write a bit
  238. 2160              GOSUB 2560                'process this address
  239. 2170           NEXT
  240. 2180           FIRST% = FALSE%
  241. 2190       'end if
  242. 2200    'end if
  243. 2210     P% = P% + 1                        'move to next pos
  244. 2220  WEND                                  'for each character.
  245. 2230 'endif
  246. 2240 RETURN
  247. 2250 '-----------------------------------------------------------------
  248. 2260 '                  Subroutine to process a command.
  249. 2270 '-----------------------------------------------------------------
  250. 2280 POSITION% = POSITION% + 1              'get the line num
  251. 2290 LINENUMBER% = ASC(MID$(A$, POSITION%, 1)) - ASC("0")
  252. 2300 POSITION% = POSITION% + 1              'get the first item
  253. 2310 CH$ = MID$(A$, POSITION%, 1)
  254. 2320 VALUE% = 0                             'default value
  255. 2330 V$ = ""                                'command string
  256. 2340 WHILE CH$ <> ESC$
  257. 2350    IF (CH$ <> " ") AND (CH$ <> ",") AND (CH$ <> CHR$(9)) THEN GOTO 2360 ELSE GOTO 2440
  258. 2360    'do
  259. 2370       IF ASC(CH$) >= ASC("0") AND ASC(CH$) <= ASC("9") THEN GOTO 2380 ELSE GOTO 2410
  260. 2380       'do
  261. 2390          VALUE% = VALUE% * 10 + ASC(CH$) - ASC("0")'convert string to integer
  262. 2400          GOTO 2430
  263. 2410       'else
  264. 2420          V$ = V$ + CH$                 'build command string
  265. 2430       'endif
  266. 2440    'endif
  267. 2450    POSITION% = POSITION% + 1           'get the next item
  268. 2460    CH$ = MID$(A$, POSITION%, 1)
  269. 2470 WEND
  270. 2480 IF V$ = DATATOKEN$ THEN GOTO 2490 ELSE GOTO 2520
  271. 2490 'do
  272. 2500    CURRLINE% = LINENUMBER%             'the user requested this line
  273. 2510    GOTO 2540
  274. 2520 'else
  275. 2530    IF VALUE% > 0 THEN COMMMANDFLAG%(LINENUMBER%) = 1 ELSE COMMMANDFLAG%(LINENUMBER%) = 0
  276. 2540 'endif
  277. 2550 RETURN
  278. 2560 '-----------------------------------------------------------------
  279. 2570 '               Subroutine to process an address.
  280. 2580 '  (a) Update the bit.
  281. 2590 '  (b) Find out if there is a command to execute.
  282. 2600 '  (c) Go to next address.
  283. 2610 '-----------------------------------------------------------------
  284. 2620 S%(CURRLINE%, CURRADDR%(CURRLINE%)) = BITVALUE%
  285. 2630 FOR LINENUMBER% = 0 TO MAXBITS%
  286. 2640    IF COMMMANDFLAG%(LINENUMBER%) = 1 THEN S%(LINENUMBER%, CURRADDR%(CURRLINE%)) = 1
  287. 2650 NEXT
  288. 2660 CURRADDR%(CURRLINE%) = CURRADDR%(CURRLINE%) + 1    'next address
  289. 2670 RETURN
  290. 2680 '-----------------------------------------------------------------
  291. 2690 '           Subroutine to calculate the maximum address.
  292. 2700 '-----------------------------------------------------------------
  293. 2710 MAXADDR% = 0 'maximum address to write to
  294. 2720 FOR I% = 0 TO MAXBITS%
  295. 2730    IF CURRADDR%(I%) > MAXADDR% THEN MAXADDR% = CURRADDR%(I%)
  296. 2740 NEXT
  297. 2750 RETURN
  298. 2760 '-----------------------------------------------------------------
  299. 2770 '   Find the name of text file and extract it from the full name
  300. 2780 '-----------------------------------------------------------------
  301. 2790 FOR P = 1 TO LEN(A$)
  302. 2800    IF MID$(A$, P, 1) = "." THEN GOTO 2820
  303. 2810 NEXT
  304. 2820 IF P < LEN(A$) + 1 THEN GOTO 2830 ELSE GOTO 2870   'a dot has been found
  305. 2830 'do
  306. 2840    FILENAME$ = LEFT$(A$, P - 1)
  307. 2850    EXT$ = RIGHT$(A$, LEN(A$) - P + 1)
  308. 2860    GOTO 2890
  309. 2870 'else
  310. 2880    FILENAME$ = LEFT$(A$, 8)
  311. 2890 'endif
  312. 2900 BINFILE$ = FILENAME$ + ".bin"
  313. 2910 HEXFILE$ = FILENAME$ + ".hex"
  314. 2920 AUDITFILE$ = FILENAME$ + ".aud"
  315. 2930 RETURN
  316. 2940 '-----------------------------------------------------------------
  317. 2950 '          All data has been read. Write bytes to a file.
  318. 2960 '  #3 is a binary file
  319. 2970 '-----------------------------------------------------------------
  320. 2980 PRINT "BIN file"
  321. 2990 OPEN BINFILE$ FOR OUTPUT AS #3
  322. 3000 FOR ADDR% = 1 TO MAXADDR% - 1
  323. 3010    VALUE% = 0
  324. 3020    FOR BIT% = MAXBITS% TO 0 STEP -1
  325. 3030       VALUE% = VALUE% * 2 + S%(BIT%, ADDR%)
  326. 3040    NEXT
  327. 3050    PRINT #3, USING "&"; CHR$(VALUE%);
  328. 3060 NEXT
  329. 3070 CLOSE #3
  330. 3080 RETURN
  331. 3090 '-----------------------------------------------------------------
  332. 3100 '  Write bytes to a file. #3 is in hex file format
  333. 3110 '-----------------------------------------------------------------
  334. 3120 PRINT "HEX file"
  335. 3130 OPEN HEXFILE$ FOR OUTPUT AS #3
  336. 3140 COUNT% = 1
  337. 3150 FOR ADDR% = 1 TO MAXADDR% - 1
  338. 3160    VALUE% = 0
  339. 3170    FOR BIT% = MAXBITS% TO 0 STEP -1
  340. 3180       VALUE% = VALUE% * 2 + S%(BIT%, ADDR%)
  341. 3190    NEXT
  342. 3200    V$ = HEX$(VALUE%)
  343. 3210    IF LEN(V$) < 2 THEN PRINT #3, USING "#& "; 0; V$;  ELSE PRINT #3, USING "& "; V$;
  344. 3220    IF COUNT% = 16 THEN PRINT #3, : COUNT% = 1 ELSE COUNT% = COUNT% + 1
  345. 3230 NEXT
  346. 3240 CLOSE #3
  347. 3250 RETURN
  348. 3260 '-----------------------------------------------------------------
  349. 3270 '  Write bytes to a file. #3 is a text file providing an audit trail
  350. 3280 '-----------------------------------------------------------------
  351. 3290 PRINT "AUD file"
  352. 3300 OPEN AUDITFILE$ FOR OUTPUT AS #3
  353. 3310 FOR ADDR% = 1 TO MAXADDR% - 1
  354. 3320    VALUE% = 0
  355. 3330    FOR BIT% = MAXBITS% TO 0 STEP -1
  356. 3340       VALUE% = VALUE% * 2 + S%(BIT%, ADDR%)
  357. 3350       PRINT #3, USING "#"; S%(BIT%, ADDR%);
  358. 3360    NEXT
  359. 3370    PRINT #3,
  360. 3380 NEXT
  361. 3390 CLOSE #3
  362. 3400 RETURN
  363. 3410 '-----------------------------------------------------------------
  364. 3420 ' Subroutine for debugging.
  365. 3430 '    Print a dump of s% on the screen
  366. 3440 '-----------------------------------------------------------------
  367. 3450 PRINT "String = "
  368. 3460 FOR BIT% = 0 TO MAXBITS%
  369. 3470    A$ = ""
  370. 3480    FOR ADDR% = 1 TO CURRADDR%(CURRLINE%) - 1
  371. 3490       A$ = A$ + STR$(S%(BIT%, ADDR%))
  372. 3500    NEXT
  373. 3510    PRINT A$
  374. 3520 NEXT
  375. 3530 RETURN
  376. 3540 END
  377.  
  378.